package com.kichina.parking.module.message.socket.message;

import com.alibaba.fastjson.JSONObject;
import com.kichina.parking.controller.parking.handler.YardStatusInfoHandler;
import com.kichina.parking.module.feign.bean.DeviceLineModel;
import com.kichina.parking.module.feign.deal.CarBanRemoteCall;
import com.kichina.parking.module.message.socket.IMessageHandler;
import com.kichina.parking.module.message.socket.SocketStorageDeal;
import com.kichina.parking.module.message.socket.bean.BaseSocketResponse;
import com.kichina.parking.module.message.socket.enums.MessageTypeEnum;
import com.kichina.parking.service.logic.bean.YardStatusInfo;
import io.vertx.core.net.NetSocket;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * @author Surging
 * @version 1.0
 * @ClassName SocketMessageAuthHandler
 * @date 2022-06-17 14:19
 * @desc 认证消息处理    eg:
 * {
 *     "service": "checkKey",
 *     "parkid": "20210001",
 *     "parkkey": "C80FB9B8-73E8-4C03-B300-2037F14F42C6"
 *   }
 */
@Component
@Order(Byte.MIN_VALUE)
@Slf4j
public class AuthHandler implements IMessageHandler {

    private final CarBanRemoteCall carBanRemoteCall;

    private final YardStatusInfoHandler yardStatusInfoHandler;

    public AuthHandler(CarBanRemoteCall carBanRemoteCall, YardStatusInfoHandler yardStatusInfoHandler) {
        this.carBanRemoteCall = carBanRemoteCall;
        this.yardStatusInfoHandler = yardStatusInfoHandler;
    }

    @Override
    public void handler(JSONObject jsonObject, NetSocket netSocket) {
        //消息实体数据不合法
        if (Objects.isNull(jsonObject.get("parkid")) ||Objects.isNull(jsonObject.get("parkkey"))){
            SocketStorageDeal.sendResponse(netSocket, BaseSocketResponse.buildAuthFailed("停车场ID或者秘钥不合法"));
            return;
        }
        log.info("Socket消息进行认证信息处理");
        //socket ID
        String socketId = netSocket.writeHandlerID();
        //车场消息
        String houseId = (String) jsonObject.get("parkkey");
        String code = (String) jsonObject.get("parkid");
        String messageStr = jsonObject.get("parkid") + "_" + houseId;
        String keyId = socketId + "_" + messageStr  ;
        //保存通道信息
        SocketStorageDeal.addNetSocket(keyId , netSocket);
        //向APP云上报设备上线信息
        DeviceLineModel deviceLineModel = new DeviceLineModel();
        deviceLineModel.setIsOnline(true);
        deviceLineModel.setHouseId(houseId);
        deviceLineModel.setCode(code);
        log.info("向APP云服务上报在线状态");
        carBanRemoteCall.cardBanRemotePost(deviceLineModel);
        YardStatusInfo yardStatusInfo = new YardStatusInfo();
        yardStatusInfo.setIsOnline(true);
        yardStatusInfo.setYardKey(code);
        yardStatusInfo.setYardPassword(houseId);
        //添加或更新车场在线状态
        yardStatusInfoHandler.saveOrUpdateYardInfo(yardStatusInfo);
        SocketStorageDeal.sendResponse(netSocket, BaseSocketResponse.buildAuthSuccess());
    }

    @Override
    public boolean check(JSONObject jsonObject) {
        return MessageTypeEnum.CHECK_KEY.getValue().equals(jsonObject.get("service"));
    }

    @Override
    public boolean interrupt() {
        return true;
    }
}
